<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery-3.6.0.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        div {
            width: 1200px;
        }
        
        .one {
            height: 400px;
            background-color: burlywood;
            border: 1px solid burlywood;
        }
        
        .two {
            height: 600px;
            background-color: pink;
            border: 1px solid pink;
        }
        
        .nav {
            position: relative;
        }
        
        .three {
            height: 300px;
            background-color: lightblue;
            border: 1px solid lightblue;
        }
        
        .four {
            height: 700px;
            background-color: yellow;
            border: 1px solid yellow;
        }
        
        .sidebar {
            width: 50px;
            height: 200px;
            background-color: violet;
            position: absolute;
            top: 50px;
            right: 2px;
        }
        
        .tops {
            width: 40px;
            height: 40px;
            background-color: #777;
            position: fixed;
            bottom: 20px;
            right: 5px;
        }
    </style>
</head>

<body>
    <div id="box">
        <div class="one"></div>

        <div class="nav">
            <div class="two"></div>
            <div class="sidebar">one</div>
        </div>
        <div class="three"></div>
        <div class="four"></div>
    </div>
    <div class="tops">

    </div>


</body>

<script>
    // javaScript写的
    //     //滑动动画
    //     function animate(obj,target,callback) {
    //     clearInterval(obj.timer)
    //     obj.timer = setInterval(function() {
    //         // var step = Math.ceil((target - obj.offsetLeft) / 10)
    //         var step = (target - window.pageYOffset) / 10;
    //         step = step > 0 ? Math.ceil(step) : Math.floor(step);
    //         if (window.pageYOffset==target) {
    //             clearInterval(obj.timer)
    //             // if(callback){
    //             //     callback();
    //             // }
    //             callback && callback()
    //         }
    //         window.scroll(0 , window.pageYOffset+step)
    //     }, 15);
    // }


    // var sidebar = document.querySelector('.sidebar')
    // var nav = document.querySelector('.nav')
    // var navTop = nav.offsetTop
    // document.addEventListener('scroll',function(){
    //     if (window.pageYOffset>=navTop) {
    //         sidebar.style.position = 'fixed'
    //         sidebar.style.left = nav.offsetWidth-52+'px'
    //         sidebar.innerHTML = "返回顶部"
    //         sidebar.addEventListener("click",function () {
    //             animate(window,0)
    //         })
    //     }else{
    //         sidebar.style.position = 'absolute'
    //         sidebar.innerHTML = "one"

    //     }
    // })
    // //返回顶部



    //jQuery写的
    $(document).ready(function () {
        // 滑动动画
        function animate(target, callback) {
            $('html, body').animate({
                scrollTop: target
            }, 200, 'swing', callback);
        }
        var $sidebar = $('.sidebar');
        var $nav = $('.nav');
        var navTop = $nav.offset().top;
        $(window).on('scroll',function () {
            if ($(window).scrollTop()>=navTop) {
                $sidebar.css({
                    'position':'fixed',
                    'left':$nav.outerWidth() -52 +'px'
                }).html("回到顶部").on('click',function () {
                    animate(0);
                    $('html,body').stop;
                })
            }else{
                    $sidebar.css('position','absolute').html("one").off('click')
        }
        })
    })
</script>

</html>